//
//  GenerateOrder.m
//  KYRR
//
//  Created by kyjun on 15/11/5.
//
//

#import "GenerateOrder.h"
#import "AppDelegate.h"
#import "GenerateOrderCell.h"
#import <CommonCrypto/CommonDigest.h>
#import "payRequsestHandler.h"
#import "AlipayOrder.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>
#import "AppDelegate.h"
#import "WXApi.h"
#import "WebPayViewController.h"

#define kShouXuFei 0.038
@interface GenerateOrder ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong) UIView* headerView;
@property(nonatomic,strong) UIButton* lineTop;
@property(nonatomic,strong) UIButton* lineBottom;
@property(nonatomic,strong) UIImageView* lineblock;
@property(nonatomic,strong) UILabel* labelUserName;
@property(nonatomic,strong) UILabel* labelAddress;
@property(nonatomic,strong) UIImageView* photoLociation;

@property(nonatomic,strong) UIButton* btnSection;

@property(nonatomic,strong) NSMutableArray* arrayData;
@property(nonatomic,strong) NSArray* arrayGoods;
@property(nonatomic,strong) NSArray* arrayRed;
/**
 *  送达时间
 */
@property(nonatomic,copy) NSString* expect_time;

@property(nonatomic,strong) UIView* footerView;
@property(nonatomic,strong) UIButton* btnSectionPay;


@property(nonatomic,strong) UIButton* btnWeXinPay;
@property(nonatomic,strong) UIImageView* imgWeiXin;
@property(nonatomic,strong) UIButton* btnAlipay;
@property(nonatomic,strong) UIImageView* imgAli;
//@property(nonatomic, strong) UIButton *btnCardPay;
//@property(nonatomic,strong) UIImageView* imgCardPay;
@property(nonatomic,strong) UIButton* btnFacePay;
@property(nonatomic,strong) UIImageView* imgFace;

@property(nonatomic,strong) UIView* sumView;
/**
 *  商品金额
 */
@property(nonatomic,strong) UILabel* labelGoodsPriceTitle;
@property(nonatomic,strong) UILabel* labelGoodsPrice;
/**
 *  满减优惠
 */
@property(nonatomic,strong) UIView* fullCutView;
@property(nonatomic,strong) NSLayoutConstraint* fullCutConstraint;
@property(nonatomic,strong) UILabel* labelCutIcon;
@property(nonatomic,strong) UILabel* labelCutTitle;
@property(nonatomic,strong) UILabel* labelFullCutPrice;
/**
 *  优惠价格
 */
@property(nonatomic,strong) UILabel* labelDiscountPriceTitle;
@property(nonatomic,strong) UILabel* labelDiscountPrice;
/**
 *  配送费
 */
@property(nonatomic,strong) UILabel* labelExpectPriceTitle;
@property(nonatomic,strong) UILabel* labelExpectPrice;
/**
 *  打包费
 */
@property(nonatomic,strong) UILabel* labelPackage;
@property(nonatomic,strong) UILabel* labelPackagePrice;

/**
 *  手续费
 */
@property(nonatomic,strong) UILabel* labelShouXu;
@property(nonatomic,strong) UILabel* labelShouXuPrice;

/**
 *  应付总金额
 */
@property(nonatomic,strong) UILabel* labelSumPriceTitle;
@property(nonatomic,strong) UILabel* labelSumPrice;
@property(nonatomic,strong) UIButton*   btnPay;

/**
 *  商品总金额
 */
@property(nonatomic,assign) double goodsSumPrice;

/**
 *  应付总金额
 */
@property(nonatomic,assign) double paySumPrice;
/**
 *  货到付款金额(线下支付金额)
 */
@property(nonatomic,assign) double offLinePrice;
/**
 *  在线支付金额
 */
@property(nonatomic,assign) double onLinePrice;

@property(nonatomic,assign) double packagePrice;

@property(nonatomic,assign) double shipPrice;
/**
 *  满减金额
 */
@property(nonatomic,assign) double fullCut;
/**
 *  优惠金额 (红包)
 */
@property(nonatomic,assign) double discountPrice;

@property(nonatomic,copy) NSString* orderNo;

@property(nonatomic,strong) NSString* remark;
@property(nonatomic,strong) UITextField* txtRemark;
/**
 *  红包选择器
 */
@property(nonatomic,strong) UIPickerView* redPickerView;
@property(nonatomic,strong) UITextField* txtRedPicker;
@property(nonatomic,assign) NSInteger redIndex;//红包下标默认为0
@property(nonatomic,assign) BOOL useRed;//是否使用红包 默认 NO


@end

@implementation GenerateOrder

-(instancetype)init{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if(self){
         
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.discountPrice = 0.00;
    self.fullCut = 0.00;
    self.remark = @"";
    [self layoutUI];
    [self layoutConstraints];
    self.redIndex = 0;
    self.useRed = NO;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    MAddress* item = delegate.globalAddress;
    self.photoLociation.image = [UIImage imageNamed:@"icon-address"];
    self.labelUserName.text = [NSString stringWithFormat:@"收货人:%@ %@",item.userName,item.phoneNum];
    self.labelAddress.text = [NSString stringWithFormat:@"收货地址:%@ %@ %@ %@",self.Identity.location.cityName,item.areaName,item.zoneName,item.address];
    
    [self refreshDataSource];
    [self btnPayWay:self.btnWeXinPay];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(paySuccessNotification:) name:NotificationPaySuccess object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payUserCancelNotification:) name:NotificationPayUserCancel object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PayFailureNotification:) name:NotificationPayFailure object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appBackNotification:) name:NotificationAppBack object:nil];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationItem.title = @"提交订单";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelTouch:)];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NotificationPaySuccess object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NotificationPayUserCancel object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NotificationPayFailure object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NotificationAppBack object:nil];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark =====================================================  试图布局
-(void)layoutUI{
    self.headerView = [[UIView alloc]init];
    self.headerView.backgroundColor = theme_default_color;
    self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 80.f);
    
    UIImage *image =[UIImage imageNamed:@"icon-line-address"];
    
    self.lineTop = [UIButton buttonWithType:UIButtonTypeCustom];
    self.lineTop.userInteractionEnabled = NO;
    self.lineTop.backgroundColor = [UIColor colorWithPatternImage:image];
    [self.headerView addSubview:self.lineTop];
    
    self.photoLociation = [[UIImageView alloc]init];
    self.photoLociation.image = [UIImage imageNamed:@"icon-location"];
    [self.headerView addSubview:self.photoLociation];
    
    self.labelUserName = [[UILabel alloc]init];
    self.labelUserName.textColor = theme_Fourm_color;
    self.labelUserName.font = [UIFont systemFontOfSize:14.f];
    [self.headerView addSubview:self.labelUserName];
    
    self.labelAddress = [[UILabel alloc]init];
    self.labelAddress.textColor = theme_Fourm_color;
    self.labelAddress.font = [UIFont systemFontOfSize:14.f];
    self.labelAddress.numberOfLines = 2;
    self.labelAddress.lineBreakMode = NSLineBreakByCharWrapping;
    [self.headerView addSubview:self.labelAddress];
    
    
    self.lineBottom = [UIButton buttonWithType:UIButtonTypeCustom];
    self.lineBottom.userInteractionEnabled = NO;
    self.lineBottom.backgroundColor = [UIColor colorWithPatternImage:image];
    [self.headerView addSubview:self.lineBottom];
    
    self.lineblock = [[UIImageView alloc]init];
    self.lineblock.backgroundColor = theme_line_color;
    [self.headerView addSubview:self.lineblock];
    
    self.footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 390)];
    self.btnSectionPay = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnSectionPay.backgroundColor = theme_default_color;
    self.btnSectionPay.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.btnSectionPay.imageEdgeInsets = UIEdgeInsetsMake(15/2, 15, 15/2, SCREEN_WIDTH-40);
    self.btnSectionPay.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [self.btnSectionPay setTitleColor:theme_Fourm_color forState:UIControlStateNormal];
    [self.btnSectionPay setTitle:@"支付方式" forState:UIControlStateNormal];
    self.btnSectionPay.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [self.footerView addSubview:self.btnSectionPay];
    
    /*
    self.btnCardPay = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnCardPay.backgroundColor = theme_default_color;
    self.btnCardPay.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.btnCardPay.imageEdgeInsets = UIEdgeInsetsMake(15/2, 15, 15/2, SCREEN_WIDTH-40);
    self.btnCardPay.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [self.btnCardPay setImage:[UIImage imageNamed:@"icon-wechat-pay"] forState:UIControlStateNormal];
    [self.btnCardPay setTitleColor:theme_Fourm_color forState:UIControlStateNormal];
    [self.btnCardPay setTitle:@"信用卡支付" forState:UIControlStateNormal];
    self.btnCardPay.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [self.btnCardPay addTarget:self action:@selector(btnPayWay:) forControlEvents:UIControlEventTouchUpInside];
    [self.footerView addSubview:self.btnCardPay];
    */
    
    self.btnWeXinPay = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnWeXinPay.backgroundColor = theme_default_color;
    self.btnWeXinPay.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.btnWeXinPay.imageEdgeInsets = UIEdgeInsetsMake(15/2, 15, 15/2, SCREEN_WIDTH-40);
    self.btnWeXinPay.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [self.btnWeXinPay setImage:[UIImage imageNamed:@"icon_card_pay"] forState:UIControlStateNormal];
    [self.btnWeXinPay setTitleColor:theme_Fourm_color forState:UIControlStateNormal];
    [self.btnWeXinPay setTitle:@"信用卡支付" forState:UIControlStateNormal];
    self.btnWeXinPay.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [self.btnWeXinPay addTarget:self action:@selector(btnPayWay:) forControlEvents:UIControlEventTouchUpInside];
    [self.footerView addSubview:self.btnWeXinPay];
    
    self.imgWeiXin = [[UIImageView alloc]init];
    [self.imgWeiXin setImage:[UIImage imageNamed:@"icon-address-default"]];
    [self.btnWeXinPay addSubview:self.imgWeiXin];
    
//    self.imgCardPay = [[UIImageView alloc]init];
//    [self.imgCardPay setImage:[UIImage imageNamed:@"icon-address-default"]];
//    [self.btnCardPay addSubview:self.imgCardPay];
    
    self.btnAlipay = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnAlipay.backgroundColor =theme_default_color;
    self.btnAlipay.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.btnAlipay.imageEdgeInsets = UIEdgeInsetsMake(15/2, 15, 15/2, SCREEN_WIDTH-40);
    self.btnAlipay.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [self.btnAlipay setImage:[UIImage imageNamed:@"icon-alipay"] forState:UIControlStateNormal];
    [self.btnAlipay setTitleColor:theme_Fourm_color forState:UIControlStateNormal];
    [self.btnAlipay setTitle:@"支付宝支付(即将开通)" forState:UIControlStateNormal];
    self.btnAlipay.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [self.btnAlipay addTarget:self action:@selector(btnPayWay:) forControlEvents:UIControlEventTouchUpInside];
    [self.footerView addSubview:self.btnAlipay];
    
    self.imgAli = [[UIImageView alloc]init];
    [self.imgAli setImage:[UIImage imageNamed:@"icon-address-default"]];
    [self.btnAlipay addSubview:self.imgAli];
    
    self.btnFacePay = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnFacePay.hidden = YES;
    [self.btnFacePay setTitleColor:theme_Fourm_color forState:UIControlStateNormal];
    NSMutableAttributedString* attributeStr = [[NSMutableAttributedString alloc]initWithString:@"货到付款(在线付款享受折扣价)"];
    
    [attributeStr addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:[@"货到付款(在线付款享受折扣价)" rangeOfString:@"在线付款享受折扣价"]];
    [self.btnFacePay setAttributedTitle:attributeStr forState:UIControlStateNormal];
    self.btnFacePay.backgroundColor =theme_default_color;
    self.btnFacePay.titleLabel.font = [UIFont systemFontOfSize:14.f];
    self.btnFacePay.contentEdgeInsets =UIEdgeInsetsMake(0, 10, 0, 0);
    self.btnFacePay.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.btnFacePay addTarget:self action:@selector(btnPayWay:) forControlEvents:UIControlEventTouchUpInside];
    [self.footerView addSubview:self.btnFacePay];
    
    self.imgFace = [[UIImageView alloc]init];
    [self.imgFace setImage:[UIImage imageNamed:@"icon-address-default"]];
    [self.btnFacePay addSubview:self.imgFace];
    
    self.sumView = [[UIView alloc]init];
    self.sumView.backgroundColor = theme_default_color;
    [self.footerView addSubview:self.sumView];
    
    self.labelGoodsPriceTitle = [[UILabel alloc]init];
    self.labelGoodsPriceTitle.text = @"商品金额:";
    [self.sumView addSubview:self.labelGoodsPriceTitle];
    
    self.labelGoodsPrice = [[UILabel alloc]init];
    self.labelGoodsPrice.text =@"$0.00";
    self.labelGoodsPrice.textAlignment = NSTextAlignmentRight;
    [self.sumView addSubview:self.labelGoodsPrice];
    
    [self.sumView addSubview:self.fullCutView];
    [self.fullCutView addSubview:self.labelCutIcon];
    [self.fullCutView addSubview:self.labelCutTitle];
    [self.fullCutView addSubview:self.labelFullCutPrice];
    
    self.labelDiscountPriceTitle = [[UILabel alloc]init];
    self.labelDiscountPriceTitle.text = @"优惠金额:";
    [self.sumView addSubview:self.labelDiscountPriceTitle];
    
    self.labelDiscountPrice = [[UILabel alloc]init];
    self.labelDiscountPrice.text = @"$0.00";
    self.labelDiscountPrice.textAlignment = NSTextAlignmentRight;
    self.labelDiscountPrice.textColor = [UIColor redColor];
    [self.sumView addSubview:self.labelDiscountPrice];
    
    self.labelExpectPriceTitle = [[UILabel alloc]init];
    self.labelExpectPriceTitle.text = @"配送费:";
    [self.sumView addSubview:self.labelExpectPriceTitle];
    
    self.labelExpectPrice = [[UILabel alloc]init];
    self.labelExpectPrice.text = @"$0.00";
    self.labelExpectPrice.textAlignment = NSTextAlignmentRight;
    [self.sumView addSubview:self.labelExpectPrice];
    
    [self.sumView addSubview:self.labelPackage];
    [self.sumView addSubview:self.labelPackagePrice];
    
    [self.sumView addSubview:self.labelShouXu];
    [self.sumView addSubview:self.labelShouXuPrice];
    
    self.labelSumPriceTitle = [[UILabel alloc]init];
    self.labelSumPriceTitle.text = @"应付总金额:";
    [self.sumView addSubview:self.labelSumPriceTitle];
    
    self.labelSumPrice = [[UILabel alloc]init];
    self.labelSumPrice.text =  @"$0.00";
    self.labelSumPrice.textColor=[UIColor redColor];
    self.labelSumPrice.textAlignment = NSTextAlignmentRight;
    [self.sumView addSubview:self.labelSumPrice];
    
    self.btnPay = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnPay.backgroundColor = [UIColor colorWithRed:229/255.f green:0/255.f blue:71/255.f alpha:1.0];
    [self.btnPay setTitleColor:theme_default_color forState:UIControlStateNormal];
    [self.btnPay setTitle:@"提交订单" forState:UIControlStateNormal];
    self.btnPay.titleLabel.font = [UIFont systemFontOfSize:20.f];
    self.btnPay.layer.masksToBounds = YES;
    self.btnPay.layer.cornerRadius = 5.f;
    [self.btnPay addTarget:self action:@selector(generalOrderTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.sumView addSubview:self.btnPay];
    
    
    
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = self.footerView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.redPickerView =[[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.redPickerView.showsSelectionIndicator = YES;
    self.redPickerView.dataSource = self;
    self.redPickerView.delegate = self;
    
    self.txtRedPicker = [[UITextField alloc]init];
    self.txtRedPicker.layer.borderColor = theme_line_color.CGColor;
    self.txtRedPicker.layer.borderWidth = 1.f;
    self.txtRedPicker.layer.masksToBounds = YES;
    self.txtRedPicker.layer.cornerRadius = 5.f;
    UIButton* btn  =[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 26, 10);
    [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
    [btn setImage:[UIImage imageNamed:@"icon-arrow-bottom-default"] forState:UIControlStateNormal];
    self.txtRedPicker.rightView = btn;
    self.txtRedPicker.rightViewMode=UITextFieldViewModeAlways;
    self.txtRedPicker.inputView =self.redPickerView;
    self.txtRedPicker.textAlignment = NSTextAlignmentCenter;
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTouched:)];
    
    // the middle button is to make the Done button align to right
    [toolBar setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
    self.txtRedPicker.inputAccessoryView = toolBar;
    
    
}
-(void)layoutConstraints{
    self.lineTop.translatesAutoresizingMaskIntoConstraints = NO;
    self.photoLociation.translatesAutoresizingMaskIntoConstraints = NO;
    self.lineBottom.translatesAutoresizingMaskIntoConstraints = NO;
    self.lineblock.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelUserName.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelAddress.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnSection.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.lineTop addConstraint:[NSLayoutConstraint constraintWithItem:self.lineTop attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:5.f]];
    [self.lineTop addConstraint:[NSLayoutConstraint constraintWithItem:self.lineTop attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.lineTop attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.lineTop attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.photoLociation addConstraint:[NSLayoutConstraint constraintWithItem:self.photoLociation attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.photoLociation addConstraint:[NSLayoutConstraint constraintWithItem:self.photoLociation attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:14.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.photoLociation attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.lineTop attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.photoLociation attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.labelUserName addConstraint:[NSLayoutConstraint constraintWithItem:self.labelUserName attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelUserName attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.lineTop attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelUserName attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.photoLociation attribute:NSLayoutAttributeRight multiplier:1.0 constant:20.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelUserName attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    [self.labelAddress addConstraint:[NSLayoutConstraint constraintWithItem:self.labelAddress attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelAddress attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.labelUserName attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelAddress attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelUserName attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelAddress attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.photoLociation attribute:NSLayoutAttributeRight multiplier:1.0 constant:20.f]];
    
    [self.lineBottom addConstraint:[NSLayoutConstraint constraintWithItem:self.lineBottom attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:5.f]];
    [self.lineBottom addConstraint:[NSLayoutConstraint constraintWithItem:self.lineBottom attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.lineBottom attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelAddress attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.lineBottom attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.lineblock addConstraint:[NSLayoutConstraint constraintWithItem:self.lineblock attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:10.f]];
    [self.lineblock addConstraint:[NSLayoutConstraint constraintWithItem:self.lineblock attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.lineblock attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.lineBottom attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.lineblock attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    self.btnSectionPay.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnWeXinPay.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnAlipay.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnFacePay.translatesAutoresizingMaskIntoConstraints = NO;
    self.imgWeiXin.translatesAutoresizingMaskIntoConstraints = NO;
    self.imgAli.translatesAutoresizingMaskIntoConstraints = NO;
    self.imgFace.translatesAutoresizingMaskIntoConstraints  = NO;
    
    self.sumView.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelGoodsPrice.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelGoodsPriceTitle.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelDiscountPrice.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelDiscountPriceTitle.translatesAutoresizingMaskIntoConstraints =NO;
    self.labelExpectPrice.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelExpectPriceTitle.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelPackage.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelPackagePrice.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelShouXu.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelShouXuPrice.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelSumPrice.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelSumPriceTitle.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnPay.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.btnSectionPay addConstraint:[NSLayoutConstraint constraintWithItem:self.btnSectionPay attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.btnSectionPay addConstraint:[NSLayoutConstraint constraintWithItem:self.btnSectionPay attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.footerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnSectionPay attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.footerView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self.footerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnSectionPay attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.footerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.btnWeXinPay addConstraint:[NSLayoutConstraint constraintWithItem:self.btnWeXinPay attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.btnWeXinPay addConstraint:[NSLayoutConstraint constraintWithItem:self.btnWeXinPay attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.footerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnWeXinPay attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.btnSectionPay attribute:NSLayoutAttributeBottom multiplier:1.0 constant:1.f]];
    [self.footerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnWeXinPay attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.footerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.imgWeiXin addConstraint:[NSLayoutConstraint constraintWithItem:self.imgWeiXin attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30.f]];
    [self.imgWeiXin addConstraint:[NSLayoutConstraint constraintWithItem:self.imgWeiXin attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30.f]];
    [self.btnWeXinPay addConstraint:[NSLayoutConstraint constraintWithItem:self.imgWeiXin attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.btnWeXinPay attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    [self.btnWeXinPay addConstraint:[NSLayoutConstraint constraintWithItem:self.imgWeiXin attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.btnWeXinPay attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    [self.btnAlipay addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAlipay attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.btnAlipay addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAlipay attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.footerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAlipay attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.btnWeXinPay attribute:NSLayoutAttributeBottom multiplier:1.0 constant:1.f]];
    [self.footerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAlipay attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.footerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.imgAli addConstraint:[NSLayoutConstraint constraintWithItem:self.imgAli attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30.f]];
    [self.imgAli addConstraint:[NSLayoutConstraint constraintWithItem:self.imgAli attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30.f]];
    [self.btnAlipay addConstraint:[NSLayoutConstraint constraintWithItem:self.imgAli attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.btnAlipay attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    [self.btnAlipay addConstraint:[NSLayoutConstraint constraintWithItem:self.imgAli attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.btnAlipay attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.sumView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:250.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.sumView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.footerView addConstraint:[NSLayoutConstraint constraintWithItem:self.sumView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.btnAlipay attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20.f]];
    [self.footerView addConstraint:[NSLayoutConstraint constraintWithItem:self.sumView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.footerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.labelGoodsPriceTitle addConstraint:[NSLayoutConstraint constraintWithItem:self.labelGoodsPriceTitle attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelGoodsPriceTitle addConstraint:[NSLayoutConstraint constraintWithItem:self.labelGoodsPriceTitle attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelGoodsPriceTitle attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelGoodsPriceTitle attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.labelGoodsPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelGoodsPrice attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelGoodsPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelGoodsPrice attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelGoodsPrice attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelGoodsPrice attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    
    self.fullCutConstraint = [NSLayoutConstraint constraintWithItem:self.fullCutView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f];
    self.fullCutConstraint.priority = 701;
    [self.fullCutView addConstraint:self.fullCutConstraint];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.fullCutView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.fullCutView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelGoodsPriceTitle attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.fullCutView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    
    [self.fullCutView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCutIcon attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.fullCutView attribute:NSLayoutAttributeHeight multiplier:0.8 constant:0.f]];
    [self.labelCutIcon addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCutIcon attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.labelCutIcon attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.f]];
    [self.fullCutView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCutIcon attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.fullCutView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    [self.fullCutView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCutIcon attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.fullCutView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.fullCutView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelFullCutPrice attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.fullCutView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.f]];
    [self.labelFullCutPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelFullCutPrice attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    [self.fullCutView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelFullCutPrice attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.fullCutView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self.fullCutView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelFullCutPrice attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.fullCutView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    [self.fullCutView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCutTitle attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.fullCutView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self.fullCutView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCutTitle attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.labelCutIcon attribute:NSLayoutAttributeRight multiplier:1.0 constant:5.f]];
    [self.fullCutView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCutTitle attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.fullCutView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.fullCutView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCutTitle attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.labelFullCutPrice attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-10.f]];
    
    
    
    [self.labelDiscountPriceTitle addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDiscountPriceTitle attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelDiscountPriceTitle addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDiscountPriceTitle attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDiscountPriceTitle attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.fullCutView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDiscountPriceTitle attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.labelDiscountPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDiscountPrice attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelDiscountPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDiscountPrice attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDiscountPrice attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.fullCutView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDiscountPrice attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    [self.labelExpectPriceTitle addConstraint:[NSLayoutConstraint constraintWithItem:self.labelExpectPriceTitle attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelExpectPriceTitle addConstraint:[NSLayoutConstraint constraintWithItem:self.labelExpectPriceTitle attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelExpectPriceTitle attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelDiscountPriceTitle attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelExpectPriceTitle attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.labelExpectPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelExpectPrice attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelExpectPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelExpectPrice attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelExpectPrice attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelDiscountPrice attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelExpectPrice attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    //打包费
    [self.labelPackage addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPackage attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelPackage addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPackage attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];

    
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPackage attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelExpectPriceTitle attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPackage attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.labelPackagePrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPackagePrice attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelPackagePrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPackagePrice attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPackagePrice attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelExpectPrice attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPackagePrice attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    //手续费
    [self.labelShouXu addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShouXu attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelShouXu addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShouXu attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShouXu attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelPackage attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShouXu attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.labelShouXuPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShouXuPrice attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelShouXuPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShouXuPrice attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShouXuPrice attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelPackagePrice attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShouXuPrice attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    //总金额
    [self.labelSumPriceTitle addConstraint:[NSLayoutConstraint constraintWithItem:self.labelSumPriceTitle attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelSumPriceTitle addConstraint:[NSLayoutConstraint constraintWithItem:self.labelSumPriceTitle attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelSumPriceTitle attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelShouXu attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelSumPriceTitle attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.labelSumPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelSumPrice attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.labelSumPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelSumPrice attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/3]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelSumPrice attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelShouXuPrice attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelSumPrice attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    
    [self.btnPay addConstraint:[NSLayoutConstraint constraintWithItem:self.btnPay attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.btnPay addConstraint:[NSLayoutConstraint constraintWithItem:self.btnPay attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH-30.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnPay attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5.f]];
    [self.sumView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnPay attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.sumView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:15.f]];
    
    
}

-(void)queryData{
    
    //cer_type 3:安卓，2：苹果；
    NSDictionary* arg = @{@"ince":@"new_order_sure",@"uid":self.Identity.userInfo.userID,@"cart_list":self.carJson,@"cer_type":@"2"};
    NetRepositories* repositories = [[NetRepositories alloc]init];
    [repositories netConfirm:arg complete:^(NSInteger react, NSDictionary *response, NSString *message) {
        [self.arrayData removeAllObjects];
        if(react == 1){
            self.expect_time = [response objectForKey:@"expect_time"];
            self.arrayGoods = [response objectForKey:@"info"];
            self.arrayRed  = [response objectForKey:@"hongbao"];
            [self.arrayData addObject:self.arrayGoods];
            [self.arrayData addObject:[[NSArray alloc]init]];
            if(self.arrayRed.count>0)
                [self.arrayData addObject:self.arrayRed];
            
            self.offLinePrice =[[response objectForKey:@"total_market_price"] doubleValue];//线下支付等于市场价格
            self.onLinePrice = [[response objectForKey:@"food_total_price"] doubleValue];//在线支付
            self.packagePrice = [[response objectForKey:@"packing_fee"] doubleValue];//打包费用
            self.shipPrice = [[response objectForKey:@"ship_fee"] doubleValue];//配送费用
            
            self.fullCut = [[response objectForKey: @"full_discount"]doubleValue]; //满减金额
            if(self.fullCut>0.00){
                /* dispatch_async(dispatch_get_main_queue(), ^{
                 if(self.fullCutConstraint){
                 [self.fullCutView removeConstraint:self.fullCutConstraint];
                 self.fullCutConstraint = [NSLayoutConstraint constraintWithItem:self.fullCutView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f];
                 self.fullCutConstraint.priority = 751;
                 [self.fullCutView addConstraint:self.fullCutConstraint];
                 }
                 });*/
            }else{
                dispatch_async(dispatch_get_main_queue(), ^{
                    if(self.fullCutConstraint){
                        [self.fullCutView removeConstraint:self.fullCutConstraint];
                        self.fullCutConstraint = [NSLayoutConstraint constraintWithItem:self.fullCutView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0.f];
                        self.fullCutConstraint.priority = 751;
                        [self.fullCutView addConstraint:self.fullCutConstraint];
                    }
                });
            }
            self.goodsSumPrice = self.onLinePrice;
            self.paySumPrice =self.onLinePrice+self.packagePrice+self.shipPrice;
            [self updatePrice];
            
        }else if(react == 400){
            [self alertHUD:message];
        }else{
            [self alertHUD:message];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }];
    
}

-(void)refreshDataSource{
    __weak typeof(self) weakSelf = (id)self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf checkNetWorkState:^(AFNetworkReachabilityStatus netWorkStatus) {
            if(netWorkStatus!=AFNetworkReachabilityStatusNotReachable){
                [weakSelf queryData];
            }else
                [weakSelf.tableView.mj_header endRefreshing];
        }];
    }];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark =====================================================  UITableView  协议实现

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if( self.btnFacePay.selected)
        return 2;
    return self.arrayData.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0 )
        return self.arrayGoods.count;
    else if (section==1)
        return 1;
    else if (section == 2 )
        return 1;
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45.f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //MStore* item = self.arrayData[section];
    UIButton* btnSection = [UIButton buttonWithType:UIButtonTypeCustom];
    btnSection.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40.f);
    btnSection.backgroundColor = theme_default_color;
    btnSection.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btnSection.imageEdgeInsets = UIEdgeInsetsMake(15/2, 15, 15/2, SCREEN_WIDTH-40);
    btnSection.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [btnSection setTitleColor:theme_Fourm_color forState:UIControlStateNormal];
    
    btnSection.titleLabel.font = [UIFont systemFontOfSize:14.f];
    btnSection.tag = section;
    CALayer *border = [CALayer layer];
    border.frame = CGRectMake(0.0f, 0.0f, SCREEN_WIDTH,1.0f);
    border.backgroundColor = theme_line_color.CGColor;
    [btnSection.layer addSublayer:border];
    border = [CALayer layer];
    border.frame = CGRectMake(0.0f, 39.0f, SCREEN_WIDTH,1.0f);
    border.backgroundColor = theme_line_color.CGColor;
    [btnSection.layer addSublayer:border];
    if(section==0){
        [btnSection setImage:[UIImage imageNamed:@"icon-store"] forState:UIControlStateNormal];
        [btnSection setTitle:@"订单信息" forState:UIControlStateNormal];
    }else if (section==1){
        [btnSection setImage:[UIImage imageNamed:@"icon-time"] forState:UIControlStateNormal];
        [btnSection setTitle:@"预计送达时间" forState:UIControlStateNormal];
        UILabel* label = [[UILabel alloc]init];
        label.frame = CGRectMake(SCREEN_WIDTH-150, 0, 140, 40);
        label.text = [NSString stringWithFormat:@"%@",self.expect_time];
        label.textColor = [UIColor redColor];
        label.font = [UIFont systemFontOfSize:12.f];
        label.textAlignment = NSTextAlignmentRight;
        [btnSection addSubview:label];
        
    }else if (section ==2){
        [btnSection setImage:[UIImage imageNamed:@"icon-preferential"] forState:UIControlStateNormal];
        [btnSection setTitle:@"使用优惠劵" forState:UIControlStateNormal];
        UILabel* label = [[UILabel alloc]init];
        label.frame = CGRectMake(SCREEN_WIDTH-150, 0, 140, 40);
        label.text = self.useRed?@"已使用":@"未使用";
        label.textColor = [UIColor redColor];
        label.font = [UIFont systemFontOfSize:14.f];
        label.textAlignment = NSTextAlignmentRight;
        [btnSection addSubview:label];
    }
    
    return btnSection;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        GenerateOrderCell* cell = (GenerateOrderCell*) [tableView dequeueReusableCellWithIdentifier:@"GenerateOrderCell"];
        if(!cell)
            cell = [[GenerateOrderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GenerateOrderCell"];
        
        [cell setItemWithDict: self.arrayGoods[indexPath.row]];
        return cell;
    }else if(indexPath.section == 1){
        UITableViewCell* cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        
        UITextField* txtRemark = [[UITextField alloc]init];
        txtRemark.frame =CGRectMake(10, 5, SCREEN_WIDTH-20, 35);
        txtRemark.layer.masksToBounds = YES;
        txtRemark.layer.cornerRadius = 5.f;
        txtRemark.backgroundColor = theme_line_color;
        txtRemark.placeholder = @"订单备注 (30字以内)";
        txtRemark.text = self.remark;
        [cell.contentView addSubview:txtRemark];
        self.txtRemark = txtRemark;
        return cell;
    }else if(indexPath.section == 2 ){
        UITableViewCell* cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        self.txtRedPicker.frame =CGRectMake(10, 5, SCREEN_WIDTH*2/3, 35);
        [cell addSubview:self.txtRedPicker];
        NSDictionary* item = self.arrayRed[self.redIndex];
        self.txtRedPicker.text =[NSString stringWithFormat:@"[%@]%@",[item objectForKey:@"type_money"],[item objectForKey:@"type_name"]];
        
        UIButton* btnUse = [UIButton buttonWithType:UIButtonTypeCustom];
        btnUse.frame = CGRectMake(SCREEN_WIDTH-90, 5, 80, 35);
        [btnUse setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [btnUse setTitleColor:[UIColor darkGrayColor] forState:UIControlStateSelected];
        [btnUse setTitle:@"使用" forState:UIControlStateNormal];
        [btnUse setTitle:@"已使用" forState:UIControlStateSelected];
        btnUse.layer.masksToBounds = YES;
        btnUse.layer.cornerRadius = 5.f;
        btnUse.layer.borderColor = theme_line_color.CGColor;
        btnUse.layer.borderWidth = 1.f;
        btnUse.selected = self.useRed;
        //btnUse.enabled = self.useRed;
        [btnUse addTarget:self action:@selector(useRedTouch:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btnUse];
        
        return cell;
        
    }
    
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark =====================================================  SEL
-(IBAction)btnPayWay:(UIButton*)sender{
    self.btnWeXinPay.selected = YES;
    [self.imgWeiXin setImage:[UIImage imageNamed:@"icon-address-enter"]];
    self.btnAlipay.selected = NO;
    self.btnFacePay.selected = NO;
    return;
    
    [self showHUD];
    self.remark = self.txtRemark.text;
    if(sender == self.btnWeXinPay){
        self.btnWeXinPay.selected = YES;
        self.btnAlipay.selected = NO;
        self.btnFacePay.selected = NO;
        [self.imgWeiXin setImage:[UIImage imageNamed:@"icon-address-enter"]];
        [self.imgAli setImage:[UIImage imageNamed:@"icon-address-default"]];
        [self.imgFace setImage:[UIImage imageNamed:@"icon-address-default"]];
        self.goodsSumPrice = self.onLinePrice;
    }else if(sender == self.btnAlipay){
        self.btnWeXinPay.selected = NO;
        self.btnAlipay.selected = YES;
        self.btnFacePay.selected = NO;
        [self.imgWeiXin setImage:[UIImage imageNamed:@"icon-address-default"]];
        [self.imgAli setImage:[UIImage imageNamed:@"icon-address-enter"]];
        [self.imgFace setImage:[UIImage imageNamed:@"icon-address-default"]];
        self.goodsSumPrice = self.onLinePrice;
    }else{
        self.btnWeXinPay.selected = NO;
        self.btnAlipay.selected = NO;
        self.btnFacePay.selected = YES;
        [self.imgWeiXin setImage:[UIImage imageNamed:@"icon-address-default"]];
        [self.imgAli setImage:[UIImage imageNamed:@"icon-address-default"]];
        [self.imgFace setImage:[UIImage imageNamed:@"icon-address-enter"]];
        self.discountPrice =0.00;
        self.goodsSumPrice = self.offLinePrice;
        self.useRed = NO;
    }
    [self updatePrice];
    [self.HUD hide:YES afterDelay:1];
}

-(IBAction)generalOrderTouch:(id)sender{
    
//    if(self.btnWeXinPay.selected){
//        if(![WXApi isWXAppInstalled]){
//            [self alertHUD:@"您的手机不支持微信支付"];
//            return;
//        }
//    }
    [self checkNetWorkState:^(AFNetworkReachabilityStatus netWorkStatus) {
        if(netWorkStatus!=AFNetworkReachabilityStatusNotReachable){
            NSString* payType =@"4";//信用卡支付// @"2";
            if(self.btnWeXinPay.selected)//微信支付
                payType = @"4";//@"2";
            else if (self.btnAlipay.selected)//支付宝支付
                payType = @"1";
            else //货到付款
                payType = @"0";
            
            
            AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
            MAddress* item = delegate.globalAddress;
            [self showHUD];
            
            
            /*begin zeng jun  2015-12-27  得到使用红包的编号 */
            NSString* bonus_use_id;
            if(self.useRed){
                NSString* emptybonus_use_id = [self.arrayRed[self.redIndex] objectForKey:@"items"];
                bonus_use_id = emptybonus_use_id;
            }else{
                bonus_use_id = @"0";
            }
            /*end zeng jun  2015-12-27*/
            
            payType = @"4";
            NSDictionary* arg = @{@"ince":@"save_order_cer",@"uid":self.Identity.userInfo.userID,@"cart_list":self.carJson,@"addr_item_id":item.rowID,@"pay_type":payType,@"order_mark":self.txtRemark.text,@"bonus_use":bonus_use_id,@"cer_type":@"2"};
            NetRepositories* repositories = [[NetRepositories alloc]init];
            [repositories netConfirm:arg complete:^(NSInteger react, NSDictionary *response, NSString *message) {
                if(react == 1){
                    [self hidHUD];
                    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationShopCarChange object:nil];
//                    if(self.btnWeXinPay.selected)//微信支付
//                        [self wxPay:[response objectForKey:@"main_id"]];
//                    else if (self.btnAlipay.selected)//支付宝支付
//                        [self aliPay:[response objectForKey:@"main_id"]];
//                    else{ //货到付款 不需要修改订单状态这里直接跳转到订单页就可以了。
//                        [self payResult:nil];
//                    }
                    WebPayViewController *webpayVC = [[WebPayViewController alloc] init];
                    webpayVC.orderid = [response objectForKey:@"orderid"];
                    [self.navigationController pushViewController:webpayVC animated:YES];
                    return;
                }else if(react == 400){
                    [self hidHUD:message];
                }else{
                    [self hidHUD:message];
                }
            }];
        }
    }];
}

-(void)cancelTouched:(UIBarButtonItem *)sender
{
    [self.txtRedPicker resignFirstResponder];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)doneTouched:(UIBarButtonItem *)sender
{
    [self.txtRedPicker resignFirstResponder];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
}
-(IBAction)useRedTouch:(id)sender{
     NSDictionary* dict=  self.arrayRed[self.redIndex];
    double limitMoney = [[dict objectForKey: @"limit_money"]doubleValue];
    if((self.goodsSumPrice-self.fullCut)>=limitMoney){
    self.remark = self.txtRemark.text;
    self.useRed = YES;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
    
    self.discountPrice =[[dict objectForKey:@"type_money"] doubleValue];
    if(self.btnFacePay.selected)
        self.goodsSumPrice=self.offLinePrice;
    else
        self.goodsSumPrice=self.onLinePrice;
    self.paySumPrice = self.goodsSumPrice;
    [self updatePrice];
    }else{
        NSString* message = [NSString stringWithFormat: @"不满%@,不能使用优惠券哦！",[dict objectForKey: @"limit_money"]];
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle: @"提示" message: message delegate:nil cancelButtonTitle: @"知道了" otherButtonTitles: nil];
        [alert show];
    }
}
-(IBAction)cancelTouch:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark =====================================================  UIPickerView 协议实现

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.arrayRed count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSDictionary *item = [self.arrayRed objectAtIndex:row];
    return [item objectForKey:@"type_name"];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.remark = self.txtRemark.text;
    if(self.useRed){
        if(self.btnFacePay.selected)
            self.goodsSumPrice=self.offLinePrice;
        else
            self.goodsSumPrice=self.onLinePrice;
        self.discountPrice = 0.00;
        self.paySumPrice = self.goodsSumPrice;
        [self updatePrice];
    }
    self.useRed = NO;
    self.redIndex = row;
}
#pragma mark =====================================================  私有方法
-(void)updatePrice{
    self.paySumPrice =self.goodsSumPrice-self.discountPrice+self.packagePrice+self.shipPrice-self.fullCut;
    self.labelFullCutPrice.text = [NSString stringWithFormat:@"-$%.2f",self.fullCut];
    self.labelDiscountPrice.text = [NSString stringWithFormat:@"-$%.2f",self.discountPrice];
    self.labelPackagePrice.text = [NSString stringWithFormat:@"$%.2f",self.packagePrice];
    NSString * shouXuPriceStr = [NSString stringWithFormat:@"%.2f",self.paySumPrice*kShouXuFei];
    self.labelShouXuPrice.text = [NSString stringWithFormat:@"$%@",shouXuPriceStr];
    self.labelExpectPrice.text = [NSString stringWithFormat:@"$%.2f",self.shipPrice];
    self.labelGoodsPrice.text = [NSString stringWithFormat:@"$%.2f",self.goodsSumPrice];
    self.labelSumPrice.text =[NSString stringWithFormat:@"$%.2f",self.paySumPrice+shouXuPriceStr.floatValue];
    [self.tableView reloadData];
}
#pragma mark 微信支付
-(void)wxPay:(NSString*)orderID{
    self.orderNo = orderID;
    //本实例只是演示签名过程， 请将该过程在商户服务器上实现
    //创建支付签名对象
    payRequsestHandler *req = [payRequsestHandler alloc];
    //初始化支付签名对象
    [req init:kWXAPP_ID mch_id:kWXPay_partnerid];
    //设置密钥
    [req setKey:kWXPay_partnerid_secret];
    
    //获取到实际调起微信支付的参数后，在app端调起支付
    NSInteger price = (NSInteger)(self.paySumPrice*100);
    NSMutableDictionary *dict = [req sendPay:orderID price:price];
    
    if(dict == nil){
        //错误提示
        NSString *debug = [req getDebugifo];
        
        [self alertHUD:debug];
        
       // NSLog(@"%@\n\n",debug);
    }else{
        //NSLog(@"%@\n\n",[req getDebugifo]);
        //[self alert:@"确认" msg:@"下单成功，点击OK后调起支付！"];
        
        NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
        
        //调起微信支付
        PayReq* req             = [[PayReq alloc] init];
        req.openID              = [dict objectForKey:@"appid"];
        req.partnerId           = [dict objectForKey:@"partnerid"];
        req.prepayId            = [dict objectForKey:@"prepayid"];
        req.nonceStr            = [dict objectForKey:@"noncestr"];
        req.timeStamp           = stamp.intValue;
        req.package             = [dict objectForKey:@"package"];
        req.sign                = [dict objectForKey:@"sign"];
        
        [WXApi sendReq:req];
    }
    
}

-(void)aliPay:(NSString*)orderID{
    self.orderNo = orderID;
    /*
     *点击获取prodcut实例并初始化订单信息
     */
    
    
    /*
     *商户的唯一的parnter和seller。
     *签约后，支付宝会为每个商户分配一个唯一的 parnter 和 seller。
     */
    
    /*============================================================================*/
    /*=======================需要填写商户app申请的===================================*/
    /*============================================================================*/
    NSString *partner = @"2088611650937514";
    NSString *seller = @"399429420@qq.com";
    NSString *privateKey = @"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKQxT+YBryNogAQlHCKNZycx5jSssV09MuOTZPU8oZCg8ap5Uhtd4Ki+MnJS/PmPbiq4cSXuc4F0KFrYroVfzyEoYvsPhdcK2bhGNsErJM1YEUXdEaCFnmJRkVhTMZ/rGUacqwfm9SJFAj8smGLYDHcu8p8GE88C0Ld8tS+R7VP1AgMBAAECgYBIartrnaiKPGfh7CApmSY64kY/Sz96+Sf9uZ+YjiJQMLwFzTaKfXA93I1RYQrUYw9a5jh5W180ZG/8CX+HsccfitNSy2FT1JVCidCDKH/Lhm+aAI1BmYm1HoDBC90+8z0G+du6qYPZQoqRB34ZKuH6aCGHAzXRCid3KVu38dnKLQJBANHUy/CsiHqNjrOUM/bidHA6n6q6PVvtVSgapFcJqTdZH8q+MLAUahYrk675Jy9zfHgeyALiiqSoRqRBbYk8IqcCQQDIUdIjP9+Qj/nut3HNPVfU+QkWjAwfPPQK9C2yHMMAe/+KwZJTWOWLAr5/F1BWwuUjdo+YXZwehxamxs24OTQDAkBU28I+awgmx+jWJzF8vshoOs0ntnnqG2k89slPidCtWZXJ4HDARQFCRg1LzedTAVltGsW4pPV88bDf7LXnsjIxAkEAhe7+2waXyaYZ61vHwPvkv8aKJwMYD8LHQJOmAdJtJuPBJdrqpMNpJhp9l6fU1U2VurJIrAzalyanJHcDkMreVQJBAJJTKn3t5NBZkNDKSvJb0cCyDWjINwKWPKgMoIGhF/my8eZrkD/Ki1BYFVN018UF7iMJPUZVZsnt3TjLHjT+RkY=";
    /*============================================================================*/
    /*============================================================================*/
    /*============================================================================*/
    
    //partner和seller获取失败,提示
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"缺少partner或者seller或者私钥。"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    AlipayOrder *order = [[AlipayOrder alloc] init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = orderID; //订单ID（由商家自行制定）
    order.productName = @"易米网店 订单"; //商品标题
    order.productDescription = @"易米i便利店 订单"; //商品描述
    order.amount = [NSString stringWithFormat:@"%.2f",self.paySumPrice]; //商品价格
    // order.notifyURL =  @"http://ezpc.zadtek.com"; //回调URL
    
    order.notifyURL =  @"http://ezpc.zadtek.com/Mobile/Tpay/notifyurl"; //回调URL
    
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme = @"com.kuyutec.xyrr";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    //NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSInteger flag = [[resultDic objectForKey:@"resultStatus"]integerValue];
            if(flag == 9000){
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationPaySuccess object:[resultDic objectForKey:@"memo"]];
            }else if(flag == 4000){
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationPayFailure object:[resultDic objectForKey:@"memo"]];
            }else {
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationPayUserCancel object:[resultDic objectForKey:@"memo"]];
            }
           // NSLog(@"%@",resultDic);
        }];
        
    }
    
}

#pragma mark =====================================================  通知
-(void)paySuccessNotification:(NSNotification*)notification{
    NSString* message = [notification object];
    [self payResult:message];
}

-(void)payUserCancelNotification:(NSNotification*)notification{
    NSString* message = [notification object];
    [self payResult:message];
}

-(void)PayFailureNotification:(NSNotification*)notification{
    NSString* message = [notification object];
    [self payResult:message];
}

-(void)appBackNotification:(NSNotification*)notification{
    [self payResult:nil];
}
-(void)payResult:(NSString*)message{
    if(message){
        /*[self showHUD:message];
         [self hidHUD:message];*/
        [self alertHUD:message];
    }
    [self dismissViewControllerAnimated:YES completion:^{
        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationChangeOrderPayStatus object:nil];
    }];
}
#pragma mark =====================================================  属性封装
-(NSMutableArray *)arrayData{
    if(!_arrayData)
        _arrayData = [[NSMutableArray alloc]init];
    return _arrayData;
}
-(UILabel *)labelShouXu{
    if(!_labelShouXu){
        _labelShouXu =[[UILabel alloc]init];
        _labelShouXu.text = @"手续费:";
    }
    return _labelShouXu;
}
-(UILabel *)labelShouXuPrice{
    if(!_labelShouXuPrice){
        _labelShouXuPrice = [[UILabel alloc]init];
        _labelShouXuPrice.text = @"$0.038";
        _labelShouXuPrice.textAlignment = NSTextAlignmentRight;
    }
    return _labelShouXuPrice;
}
-(UILabel *)labelPackage{
    if(!_labelPackage){
        _labelPackage =[[UILabel alloc]init];
        _labelPackage.text = @"打包费:";
    }
    return _labelPackage;
}

-(UILabel *)labelPackagePrice{
    if(!_labelPackagePrice){
        _labelPackagePrice = [[UILabel alloc]init];
        _labelPackagePrice.text = @"$0.00";
        _labelPackagePrice.textAlignment = NSTextAlignmentRight;
    }
    return _labelPackagePrice;
}

-(UIView *)fullCutView{
    if(!_fullCutView){
        _fullCutView = [[UIView alloc]init];
        _fullCutView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _fullCutView;
}

-(UILabel *)labelCutIcon{
    if(!_labelCutIcon){
        _labelCutIcon = [[UILabel alloc]init];
        _labelCutIcon.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: @"icon-full-cut"]];;
        _labelCutIcon.font = [UIFont systemFontOfSize:14.f];
        _labelCutIcon.textColor = [UIColor whiteColor];
        _labelCutIcon.layer.masksToBounds = YES;
        _labelCutIcon.layer.cornerRadius = 3.f;
        _labelCutIcon.textAlignment = NSTextAlignmentCenter;
        _labelCutIcon.text =  @"减";
        _labelCutIcon.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _labelCutIcon;
}

-(UILabel *)labelCutTitle{
    if(!_labelCutTitle){
        _labelCutTitle = [[UILabel alloc]init];
        _labelCutTitle.text =  @"满减优惠:";
        _labelCutTitle.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _labelCutTitle;
}

-(UILabel *)labelFullCutPrice{
    if(!_labelFullCutPrice){
        _labelFullCutPrice = [[UILabel alloc]init];
        _labelFullCutPrice.text = @"$0.00";
        _labelFullCutPrice.textAlignment = NSTextAlignmentRight;
        _labelFullCutPrice.textColor = [UIColor redColor];
        _labelFullCutPrice.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _labelFullCutPrice;
}

@end
